//
//  ActiveViewController.m
//  豆瓣masonry
//
//  Created by dllo on 15/12/14.
//  Copyright © 2015年 SMH. All rights reserved.
//

#import "ActiveViewController.h"
#import "DetailViewController.h"

@interface ActiveViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)NSMutableArray *arr;

@property (nonatomic, retain)UITableView *tableView;


@end

@implementation ActiveViewController

- (void)handleData {
    

    NSString *path = [[NSBundle mainBundle]pathForResource:@"activitylist" ofType:@"txt"];

    NSData *data = [NSData dataWithContentsOfFile:path];
    

    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

    NSArray *array = [result objectForKey:@"events"];
    

    self.arr = [NSMutableArray array];
    

    for (NSDictionary *dic in array) {
        Model *model = [Model modelWithDic:dic];
        [self.arr addObject:model];
    }
    
    
}


- (void)createTableView {
    
    //    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    

    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.mas_equalTo(0);
        make.top.left.mas_equalTo(64);
        
    }];
    
    

    [self.tableView registerClass:[ActiveTableViewCell class] forCellReuseIdentifier:@"activeTableViewCell"];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model *model = [self.arr objectAtIndex:indexPath.row];
    
    ActiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activeTableViewCell"];
    cell.model = model;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 180.0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailViewController *detail = [[DetailViewController alloc] init];
    

    detail.model = [self.arr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:detail animated:YES];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"活动";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:86 / 255.0 green:137 / 255.0 blue:156 / 255.0 alpha:1];
    
    [self handleData];
    [self createTableView];

    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
